﻿using System;
using System.Windows.Forms;

namespace SBPweb.RouTorrent.Service
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            Program.mainService.DownloadManager.OnDownloadStateChanged += new SBPweb.RouTorrent.Business.DownloadStateChangedHandler(DownloadManager_OnDownloadStateChanged);
            Program.mainService.DownloadManager.OnPieceStateChanged += new SBPweb.RouTorrent.Business.PieceStateChangedHandler(DownloadManager_OnPieceStateChanged);
            Program.mainService.DownloadManager.OnPeerStateChanged += new SBPweb.RouTorrent.Business.PeerStateChangedHandler(DownloadManager_OnPeerStateChanged);

            Program.mainService.DownloadManager.QueueDownload(0, @"c:\Documents and Settings\lszacsuri\Desktop\[nCore][xvidser]Stargate.Atlantis.S04E13.DSR.XviD-0TV.torrent");

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void AppendText(string text)
        {
            if (this.richTextBox1.InvokeRequired)
            {
                this.richTextBox1.Invoke(new StringParameteredHandler(AppendText), text);
                return;
            }

            this.richTextBox1.AppendText(text);
        }


        private void DownloadManager_OnDownloadStateChanged(object sender, SBPweb.RouTorrent.Business.DownloadStateChangedEventArgs e)
        {
            AppendText(string.Format("{0}: {1} {2}\n", e.UserId, e.DownloadManager.Torrent.Name, e.NewState));
        }

        private void DownloadManager_OnPieceStateChanged(object sender, SBPweb.RouTorrent.Business.PieceStateChangedEventArgs e)
        {
            AppendText(string.Format("?: {0} {1} {2}\n", e.DownloadManager.Torrent.Name, e.PieceIndex, e.HashPassed));
        }

        private void DownloadManager_OnPeerStateChanged(object sender, SBPweb.RouTorrent.Business.PeerStateChangedEventArgs e)
        {
            AppendText(string.Format("?: {0} {1}\n", e.DownloadManager.Torrent.Name, e.PieceCount));
        }

        public delegate void StringParameteredHandler(string s);
    }
}